<template>
  <basic-container>
    <div class="app-container calendar-list-container">
      <avue-form ref="form" v-model="form" :option="option" @submit="submitJbxx">
      </avue-form>
    </div>
  </basic-container>
</template>
<script>
import { mapGetters } from 'vuex'
import { getStore } from '@/util/store'
import option from '@/const/cust/shzlnl'
import { addObj, updateObj, getObj } from '@/api/cust/health/shzlnl'
export default {
  data() {
    return {
      option: option,
      jktjid: this.$route.params.id,
      grjbxxid: this.$route.query.grjbxxid,
      jkdn: getStore({ name: 'jkdn' }),
      form: {
        zdfhj: 0,
        grjbxxid: this.grjbxxid,
        jktjid: this.jktjid,
        lnrpgjincan: '0',
        lnrpgshufa: '0',
        lnrpgchuanyi: '0',
        lnrpgruce: '0',
        lnrpghuodong: '0'
      }
    }
  },
  watch: {
    form: {
      handler: function() {
        this.sum()
      },
      deep: true
    }
  },

  computed: {
    ...mapGetters(['permissions'])
  },
  created() {
    if (!this.validatenull(this.jktjid) && !this.validatenull(this.grjbxxid)) {
      this.getCust()
    }
  },
  methods: {
    getCust() {
      getObj({ grjbxxid: this.grjbxxid, jktjid: this.jktjid }).then(res => {
        if (!this.validatenull(res.data.data)) {
          this.form = res.data.data[0]
        }
      })
    },
    sum() {
      this.form.zdfhj =
        Number(this.form.lnrpgjincan) +
        Number(this.form.lnrpgshufa) +
        Number(this.form.lnrpgchuanyi) +
        Number(this.form.lnrpgruce) +
        Number(this.form.lnrpghuodong)
    },
    submitJbxx() {
      if (this.validatenull(this.form.id)) {
        //新增
        this.form.grjbxxid = this.grjbxxid
        this.form.jktjid = this.jktjid
        this.form.name = this.jkdn.name
        this.form.tjbh = this.jkdn.dnbh
        addObj(this.form).then(res => {
          this.form = res.data
          this.$message({
            showClose: true,
            message: '提交成功',
            type: 'success'
          })
        })
      } else {
        //更新
        updateObj(this.form).then(() => {
          this.$message({
            showClose: true,
            message: '更新成功',
            type: 'success'
          })
        })
      }
    }
  }
}
</script>